//
//  UserCenter.m
//  PalmHospitalTongJi
//
//  Created by leon on 13-11-20.
//  Copyright (c) 2013年 lvxian. All rights reserved.
//

#import "UserCenter.h"
#import "AllNotificationKeys.h"
@interface UserCenter(){
//    taskProfile*        _taskProfileData;
//    CDTTaskGetShengFenLieBiao *_taskGetShengFenLieBiao;
}
@end

@implementation UserCenter
@synthesize authToken = _authToken;
@synthesize userName = _userName;
@synthesize userId = _userId;
@synthesize isLogined = _isLogined;
@synthesize user = _user;

+ (instancetype)shareCenter
{
    static UserCenter* center;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        center = [[UserCenter alloc] init];
    });
    return center;
}

- (id)init
{
    if(self = [super init]){
//        _user = [self getProfileDisk];
//        _authToken = [[NSUserDefaults standardUserDefaults] objectForKey:SAVE_KEY_AUTH];
    }
    return self;
}

- (NSString*)authToken
{
    if(!_authToken){
        _authToken = @"";
    }
    return _authToken;
}

//- (NSString*)userName
//{
//    if(self.user) return self.user.userName;
//    return @"";
//}
//
//- (NSString *)userId
//{
//    if(self.user) return [NSString stringWithFormat:@"%@",self.user.userId];
//    return @"";
//}


//true:已登录，false:未登录
- (BOOL)isLogined
{
    return _user != nil;
}
//
//- (void)loginUserName:(NSString*)userName
//               passwd:(NSString*)passwd
//           completion:(void (^)(modelLogin *response, NSError *error))completion
//{
//    NSString *device = [[NSUserDefaults standardUserDefaults] objectForKey:USERDEFAULTS_NOTIFY_DEVICE_NO];
//    if(!device) device = @"";
//    NSDictionary *dict = [NSDictionary new];
//    dict = @{ @"loginId":userName,@"password":passwd,@"deviceId":device};
//    PMLEnvironmentConfig* pmConfig= [PMLEnvironmentConfig defaultConfig];
//    pmConfig.requestHeaderAuthentication =@"";
//    [self clearUserState];
//    [[taskUserLogin sharedClient] loginWithParams:dict completion:^(modelLogin *response, NSError *error) {
//        if (error==nil) {
//            // 设置用户token
//            _authToken = response.authentication;
//            [PMLEnvironmentConfig defaultConfig].requestHeaderAuthentication =[[UserCenter shareCenter]authToken];
//            [[NSUserDefaults standardUserDefaults] setObject:_authToken forKey:SAVE_KEY_AUTH];
//
//            //设置消息数
//            [MessageEchat setMessageNum];
//
//
//            // 登录成功刷新本地存储
//            [[NSUserDefaults standardUserDefaults] removeObjectForKey:SAVE_KEY_PROFILE];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//            [self getProfileRemote:^(Profile *profile, NSError *error) {
//                _user = profile;
//                //获取用户消息标示
////                XiaoXiViewController* controlleer = [[XiaoXiViewController alloc] init];
//#warning 更新用户消息数，暂时注释
////                [controlleer getMessageNum];
//
//                if (completion) {
//                    return completion(response,error);
//                }
//            }];
//        }else{
//            if (completion) {
//                NSLog(@"%@",response.message);
//
//                return completion(response,error);
//            }
//        }
//
//    }];
//}

#pragma mark - 退出登录

//- (void)logout:(void (^)(PMLResponseModelBase* error))completion{
//    PMLResponseModelBase  *result = [[PMLResponseModelBase alloc] init];
//    [[taskUserLogin  sharedClient] userLoginOutWithParams:@{} completion:^(PMLResponseModelBase *response, NSError *error) {
//        [MixProgressHUD dismiss];
//
//        if (error == nil) {
//            result.code = GHResponseCodeSuccess;
//            if (_user && _authToken) {
//                [[NSNotificationCenter defaultCenter] postNotificationName:NKUserDidLogout object:nil];
//                result.flag = GHResponseCodeSuccess;
//                PMLEnvironmentConfig*  pmtemp=[PMLEnvironmentConfig defaultConfig];
//                pmtemp.requestHeaderAuthentication =@"";
//                result.message = @"退出登录成功";
//                pmtemp.available = YES;
//                [[NSUserDefaults standardUserDefaults] removeObjectForKey:APP_SOCKET_MESSAGE_NUM];
//                [MessageEchat setApplicationNum];    //清除图标数字
//            }else{
//              //
//            }
//        }else{
//            //
//        }
//        [self clearUserState];
//        if(completion){
//            completion(response);
//        }
//    }];
//}

-(void)clearUserState{
//    _user = nil;
//    _authToken = nil;
//
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SAVE_KEY_AUTH];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SAVE_KEY_PROFILE];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:SAVE_KEY_MEMID];
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passwd"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
//
//    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATIONNAME_LOGOUT object:nil];
}

#pragma mark - 忘记密码
//- (void)verifyForgetPwdParams:(NSDictionary*)params completion:(void (^)(PMLResponseModelBase* base,NSError *error))completion{
//
//    [[taskCheckCode sharedClient] verifyCheckCodeWithParams:params completion:^(PMLResponseModelBase *base, NSError *error) {
//        if (completion) {
//            completion(base,error);
//        }
//    }];
//
//}

#pragma mark - 发送短信息
//- (void)sendCheckCodeParams:(NSDictionary*)params
//                 completion:(void (^)(PMLResponseModelBase* base,NSError *error))completion{
//    //taskCheckCode *taskcode = [taskCheckCode new];
//    [[taskCheckCode sharedClient] sendCheckCodeWithParams:params completion:^( PMLResponseModelBase* base,NSError *error) {
//        if (completion) {
//            completion(base,error);
//        }
//    }];
//}


/**
 *  验证注册验证码及注册
 *
 */
//- (void)verifyRegisterCheckCodeParams:(NSDictionary*)params
//                           completion:(void (^)(PMLResponseModelBase* base,NSError *error))completion{
//    //taskCheckCode *taskcode = [taskCheckCode new];
//    [[taskCheckCode sharedClient] verifyRegisterCheckCodeWithParams:params completion:^( PMLResponseModelBase* base,NSError *error) {
//        if (completion) {
//            completion(base,error);
//        }
//    }];
//}


#pragma mark - user profile info


/**
 *  获取用户信息---调接口
 *
 */
//- (void)getProfileRemote:(void (^)(Profile * profile, NSError *error))completiton{
//
////    [[taskProfile sharedClient] getProfileInfo:@{@"showRealNameVerify":@1,@"showBankVerify":@1} completion:^(Profile *response, NSError *error) {
//    [[taskProfile sharedClient] getProfileInfo:@{@"showRealNameVerify":@1,@"showBankVerify":@1} completion:^(Profile *response, NSError *error) {
//        Profile* proFile = [[Profile alloc]init];
//        if (error == nil) {
//            proFile = response;
//
//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                NSData* userObj = [NSKeyedArchiver archivedDataWithRootObject:proFile];
//                if(userObj){
//                    [[NSUserDefaults standardUserDefaults] setObject:userObj forKey:SAVE_KEY_PROFILE];
//                    [[NSUserDefaults standardUserDefaults] synchronize];
//                }
//            });
//        }
//#warning to show login window, because get profile fail
//        if (completiton) {
//            completiton(proFile,error);
//        }
//    }];
//
//
//}

/**
 *  获取用户信息----取本地数据--
 *
 */
//- (Profile *)getProfileDisk
//{
//    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:SAVE_KEY_PROFILE];
//    if (data) {
//        Profile *profile = (Profile *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
//        return profile;
//    }
//    return nil;
//}

/**
 *  刷新用户信息本地数据存储
 *
 *  @param completiton <#completiton description#>
 */
//- (void)refreshProfiles:(void (^)(Profile * profile))completiton
//{
//    [self getProfileRemote:^(Profile *profile, NSError *error) {
//        _user = profile;
//        if(completiton){
//            if (error != nil) {
//                profile = nil;
//            }
//            completiton(profile);
//        }
//    }];
//}

/**
 *  获取用户信息---自动筛选
 *
 */
//- (void)getProfiles:(void (^)(Profile * profile))completiton{
//    Profile *profile = [self getProfileDisk];
//    if (profile) {
//        if(completiton){
//            completiton(profile);
//        }
//    }else{
//        [self getProfileRemote:^(Profile *profile, NSError *error) {
//            if(completiton){
//                if (error != nil) {
//                    profile = nil;
//                }
//                completiton(profile);
//            }
//        }];
//    }
//}


/**
 *  获取用户信息---调接口
 *
 */
//- (void)gethealthCardRemote:(void (^)(healthCard * profile,NSError *error))completiton{
//
//    [[taskHealthCard sharedClient] getHealthCardListWithParams:@{} completion:^(healthCard *response, NSError *error) {
//        if (error == nil) {
//
//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//                NSData* userObj = [NSKeyedArchiver archivedDataWithRootObject:response];
//                if(userObj){
//                    [[NSUserDefaults standardUserDefaults] setObject:userObj forKey:SAVE_KEY_healthCard];
//                    [[NSUserDefaults standardUserDefaults] synchronize];
//                }
//            });
//        }
//        if (completiton) {
//            completiton(response,error);
//        }
//    }];
//
//}

/**
 *  获取用户信息----取本地数据--
 *
 */
//- (healthCard *)gethealthcardDisk
//{
//    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:SAVE_KEY_healthCard];
//    if (data) {
//        healthCard *profile = (healthCard *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
//        return profile;
//    }
//    return nil;
//}

@end
